package com.lecture.javamode.singleton;

public class SingletonDemo2 {

    private static volatile SingletonDemo2 singletonDemo2;

    private String name;

    private SingletonDemo2() {
        name = "张三";
    }

    public static SingletonDemo2 getInstance() {
        SingletonDemo2 tmp = singletonDemo2;
        if (tmp == null) {
            synchronized (SingletonDemo2.class) {
                tmp = singletonDemo2;
                if (tmp == null) {
                    tmp = singletonDemo2 = new SingletonDemo2();
                }
            }
        }
        return tmp;
    }

    public static SingletonDemo2 getInstance2() {
        if (singletonDemo2 == null) {
            synchronized (SingletonDemo2.class) {
                if (singletonDemo2 == null) {
                    singletonDemo2 = new SingletonDemo2();
                }
            }
        }
        return singletonDemo2;

    }

    public String getName() {
        return name;
    }

}
